package cn.pegasus.teamwork.appinterface.controller.user;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import cn.iyizhan.teamwork.base.util.ObjectUtil;
import cn.pegasus.teamwork.appinterface.base.BaseController;
import cn.pegasus.teamwork.appinterface.util.DateUtils;
import cn.pegasus.teamwork.appinterface.util.ParameterEunm;
import cn.pegasus.teamwork.appinterface.util.ReturnRsUtils;
import cn.pegasus.teamwork.pegasus.entity.BussUserCars;
import cn.pegasus.teamwork.pegasus.service.BussUserCarsService;

/**
 * @ClassName: SysUcenerUserController
 * @Description: 用户控制中心
 * @author ryan.bian
 * @date 2017年11月12日 上午10:10:32
 * 
 *       包括以下主要几个接口：
 * 
 *       ===================================== 
 *       1.车辆信息列表 
 *       2.车辆信息详情 
 *       3.添加车辆 
 *       4.修改车辆信息
 *       5.删除车辆信息
 *       ===========================================
 * 
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
@RequestMapping("/car/*")
public class UserCarInfoController extends BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(UserCarInfoController.class);

	/**
	 * 车辆管理
	 */
	@Autowired
	private BussUserCarsService bussUserCarsService;

	/**
	 * 车辆信息列表
	 * 
	 * @param map
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/cils", method = RequestMethod.POST)
	public Map<String, Object> carinfoList(@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			map.put("userId", getSessionUserId());
			List<BussUserCars> bucs = bussUserCarsService.query(map);
			if (bucs != null && bucs.size() > 0) {
				Map<String, Object> mape = new HashMap<String, Object>();
				mape.put("lis", bucs);
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, mape);
			} else {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE, null);
			}

		} catch (Exception e) {
			LOGGER.error("服务器异常!", e);
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_403_CODE, null);
		}
		LOGGER.info("返回参数 ：{}", JSONObject.toJSON(resultMap));
		return resultMap;
	}

	/**
	 * 车辆信息详情
	 * 
	 * @param map
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/ci", method = RequestMethod.POST)
	public Map<String, Object> carinfo(@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		try {
			boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "id" }, map);
			if (isNotNull) {
				BussUserCars buc = new BussUserCars();
				buc = bussUserCarsService.detail(map);
				if (buc != null) {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, buc);
				} else {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE, null);
				}
			} else {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, null);
			}

		} catch (Exception e) {
			LOGGER.error("服务器异常!", e);
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_403_CODE, null);
		}
		LOGGER.info("返回参数 ：{}", JSONObject.toJSON(resultMap));
		return resultMap;
	}

	/**
	 * 添加车辆
	 * 
	 * @param userName
	 * @param password
	 * @param app_code
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/ac", method = RequestMethod.POST)
	public Map<String, Object> addCar(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		try {
			boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "carCode", "carBrand", "carColor" }, map);
			if (isNotNull) {
				map.put("userId", getSessionUserId());
				BussUserCars buc = createCatInfo(map);
				if (buc != null) {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, true);
				} else {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE, null);
				}
			} else {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, null);
			}

		} catch (Exception e) {
			LOGGER.error("服务器异常!", e);
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_403_CODE, null);
		}
		LOGGER.info("返回参数 ：{}", JSONObject.toJSON(resultMap));
		return resultMap;
	}

	/**
	 * 修改车辆信息
	 * 
	 * @param id
	 * @param map
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/uc", method = RequestMethod.POST)
	public Map<String, Object> updateCar(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "id", "carCode", "carBrand", "carColor" },
					map);
			if (isNotNull) {
				map.put("userId", getSessionUserId());
				bussUserCarsService.update(map);

				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, true);

			} else {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, null);
			}

		} catch (Exception e) {
			LOGGER.error("修改用户失败.", e);
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_500_CODE, null);
		}
		LOGGER.info("返回参数 ：{}", JSONObject.toJSON(resultMap));
		return resultMap;
	}

	/**
	 * 删除车辆信息
	 * 
	 * @param phone_number
	 * @param app_code
	 * @return
	 */
	@RequestMapping(value = "/dc", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteCar(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "id" }, map);
			if (isNotNull) {
				map.put("userId", getSessionUserId());
				bussUserCarsService.delete(map);
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, true);
			} else {
				LOGGER.error("数据参数校验不合法，请输入正确的参数");
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, null);
			}

		} catch (Exception e) {
			LOGGER.error("验证手机号码失败.", e);
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_500_CODE, null);
		}
		LOGGER.info("返回参数 ：{}", JSONObject.toJSON(resultMap));
		return resultMap;
	}

	/**
	 * 添加用户车辆信息
	 * 
	 * @param orderVo
	 */
	public BussUserCars createCatInfo(Map<String, Object> map) {

		try {
			BussUserCars buc = new BussUserCars();
			buc.setUserId(Long.valueOf(map.get("userId").toString()));
			// 预约服务地址
			if (map.get("areaAddress") != null) {
				buc.setAreaAddress(map.get("areaAddress").toString());
			}

			buc.setCarCode(map.get("carCode").toString()); // 车牌号

			// 车辆图片
			if (map.get("carBrand") != null) {
				buc.setCarBrand(map.get("carBrand").toString());
			}

			// 车辆图片
			if (map.get("carColor") != null) {
				buc.setCarColor(map.get("carColor").toString());
			}
			
			// 车辆图片
			if (map.get("carModel") != null) {
				buc.setCarModel(map.get("carModel").toString());
			}

			// 车辆图片
			if (map.get("carImages") != null) {
				buc.setCarImages(map.get("carImages").toString());
			}

			if (map.get("linkMan") != null) {
				buc.setLinkMan(map.get("linkMan").toString()); // 联系人
			}

			if (map.get("linkTel") != null) {
				buc.setLinkTel(map.get("linkTel").toString()); // 联系电话
			}
			buc.setCreateTime(DateUtils.getNowDate());// 订单创建时间',
			buc.setCreateUserId(Long.valueOf(map.get("userId").toString()));// 创建用户id',
			BussUserCars nbuc = bussUserCarsService.add(buc);
			if (nbuc != null) {
				return nbuc;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
